package newXgame.hall.csuper
{
	import anshe.struct.UserInfo;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import newXgame.hall.events.TableEvent;
	import newXgame.hall.object.tables.PlayerSeatObject;
	
	import org.dstart.draw.csuper.CSprite;

	public class SuperRoomTable extends CSprite
	{
		// protected var nowSeatID:Number;		//	当前的坐位号
		private var nowTableID:Number;		//	当前的桌号
		private var seatArray:Array;		//	坐位数组
		private var userInfo:UserInfo;		//	用户信息
		
		public var sprBgHot:Sprite;			//	底部区块（用于决定对象的大小）
		public var sprTable:MovieClip;		//	桌面（几帧，代表几种状态）
		
		public function SuperRoomTable()
		{
			try
			{
				this.nowTableID = 0;
				this.sprTable.gotoAndStop(1);		
				
				this._root.addChild(this.sprBgHot);
				this._root.addChild(this.sprTable);
				
				this.sprTable.mouseChildren = this.sprTable.mouseEnabled = false;
				this.sprBgHot.mouseChildren = this.sprBgHot.mouseEnabled = false;
			}
			catch(e:*)
			{
				throw new Error("Can't find Object by RoomTable");
			}
		}
		
		//	设置当前桌号
		public function SetTableID(num:Number, uInfo:UserInfo):void
		{
			this.nowTableID = num;
			this.userInfo = uInfo;
		}
		
		//	设置坐位数组
		protected function SetSeatArray(array:Array):void
		{
			if(array)
			{
				this.seatArray = array;
				for(var i:int = 0; i < this.seatArray.length; i++)
				{
					try
					{					 
						AddSeatListener(this.seatArray[i]);						
					}
					catch(err:*)
					{
						throw new Error("Set Table Object Error!!!");
					}
				}
			}
		}
		
		//	为指定的坐位添加事件
		protected function AddSeatListener(sObj:PlayerSeatObject):void
		{
			this._root.addChild(sObj);
			
			sObj.addEventListener(MouseEvent.CLICK, onUserSeatEvent);
		}
		
		//	当点击坐位时触发事件
		private function onUserSeatEvent(e:MouseEvent):void
		{
			var sObj:PlayerSeatObject = e.target as PlayerSeatObject;
			if(sObj)
			{
				if(!sObj.info && this.nowTableID > 0 && this.userInfo)
				{
					sObj.info = this.userInfo;
					
					this.dispatchEvent(new TableEvent(TableEvent.SEAT_DOWN, this.nowTableID, sObj.seatID));
				}
			}
		}
	}
}